<?php 
class ShelfAction extends BaseAction {
	
	
	/**
	 *用于显示上架商品列表
	 */
	public function index() {
		
		$map['p_sort_id']			= $_REQUEST['p_sort_id'];		//主分类	对应页面左侧主分类
		$map['g_sort_id']			= $_REQUEST['g_sort_id'];	//子分类	对应页面左侧子分类
		$map['brandid']				= $_REQUEST['partner_id'];	//商户id		对应页面右侧主搜索区
		$map['bprice']				= $_REQUEST['bprice'];	//价格上限	对应页面右侧主搜索区
		$map['sprice']				= $_REQUEST['sprice'];	//价格下限	对应页面右侧主搜索区
		$map['stype']				= $_REQUEST['stype'];		//排序类型	对应页面右侧主搜索区 'sale' 'new' 'up'  'down'
		$map['pno']					= $_REQUEST['pno'];			//页数
		$map['shelf_id']			= $_REQUEST['shelf_id'];			
		$map['goods_id']			= $_REQUEST['goods_id'];		
		$map['shelfstatus']		= $_REQUEST['shelfstatus'];
		
		//if(empty($map['stype']))$map['stype']	= 'sale';
		if(empty($map['pno']))$pno	= 1;
		if(!empty($map['g_sort_id']))$map['sortcode']=array($map['g_sort_id']);
		if(!empty($map['p_sort_id']))$map['p_sortcode']=array($map['p_sort_id']);
			
		/**用来处理带有条件的查询**/
		$shelfstatus 	= $_REQUEST ['shelfstatus'];//说明：on表示上架商品，off下架的商品，delete删除的上架商品
		//if(empty($shelfstatus))$shelfstatus='on';
		if(!empty($_REQUEST['g_enable'])){
			$map['goods_enable']=$_REQUEST['g_enable'];
		}
		if(!empty($_REQUEST['p_enable'])){
			$map['partner_enable']=$_REQUEST['p_enable'];
		}
		$map['partner_is_delete']='N';
		$map['shelf_is_delete']='N';
		$map['goods_is_delete']='N';
		
		$shelfpage = service( 'Product' )->search4Sys($map,20);
		//dump($shelfpage);
		$this->assignRequestParams();
		$this->assign( 'shelfpage', $shelfpage );
		$this->display();

	}
	
	
	/**
	 *显示已经下架的商品列表
	 */
	public function offlist() {
		;
	}
	
	
	/**
	 *新增上架商品
	 */
	public function puton() {
		$this -> display();
	}
	
	
	/**
	 *新增上架商品录入到数据库
	 */
	public function doPuton() {
			$map['goods_id'] 		= intval( $_POST['goods_id'] );
			$map['out_price'] 		= floatval( $_POST['out_price'] );
			$map['market_price'] 	= floatval( $_POST['market_price'] );
			$map['total_number'] 	= intval( $_POST['total_number'] );
			$map['gain_credit'] 	= intval( $_POST['gain_credit'] );
			$map['sort_id'] 		= $_POST['sort_id'] ;
			
			$map['on_time'] 	= paramDate( $_POST['on_time'] );
			$map['off_time'] 	= paramDate( $_POST['off_time']  );
				
			if(isset($_POST['enable'])) $map['enable'] = 'Y'; else $map['enable'] = 'N';
				
			//参数合法性检查
			$required_field = array(
					'goods_id'		=> '商品',
					'on_time'	=> '上架日',
					'off_time'	=> '下架日',
					'out_price'=> '买出价格',
					'sort_id'		=> '上架分类'
			);
			foreach ($required_field as $k => $v) {
					if ( empty($map[$k]) ){
							$this->error($v . '不可为空');
					}
			}
			
			if($map['on_time'] < (time()-24*60*60)){
					$this->error('上架日期需大于当前日期');
			}
			
			if($map['off_time'] <= $map['on_time']){
					$this->error('上架日期需大于下架日期');
			}

		  $map['ctime'] 			=	time();
		  //$map['send_number'] 			=	0;
		  //$map['is_delete'] 	= 'N';
		  $map['insert_admin_id']	=	$this->adminId;
		  $shelfDao = D('GoodsShelf');

		  $id = $shelfDao->add($map);
		  if(!empty($id)){
					$this->success('新增成功！');
			}else{
					
			}
			$this->error('新增失败！');
	}
	
	
	/**
	 *新增上架商品
	 */
	public function edit() {
			$map['shelf_id'] 			= intval( $_REQUEST['shelf_id'] );
			
			if ( empty($map['shelf_id']) ){
					$this->error('参数有误，为选择对应上架！');
			}
			$shelfDao = D('GoodsShelf');

		  $shelf = $shelfDao->where(" id=".$map['shelf_id']." and is_delete='N'")->find();
			if ( empty($shelf) )$this->error('对应上架不存在或已删除！');
			$this->assign( 'shelf', $shelf );
			$this->assign( 'type', 'edit' );
			$this -> display('puton');
	}
	
	
	/**
	 *新增上架商品录入到数据库
	 */
	public function doEdit() {
			$id 			= intval( $_POST['id'] );
			//$map['goods_id'] 			= intval( $_POST['goods_id'] );
			$map['out_price'] 		= floatval( $_POST['out_price'] );
			$map['market_price'] 	= floatval( $_POST['market_price'] );
			$map['total_number'] 	= intval( $_POST['total_number'] );
			$map['gain_credit'] 	= intval( $_POST['gain_credit'] );
			$map['sort_id'] 			= $_POST['sort_id'];
			
			$map['on_time'] 	= paramDate( $_POST['on_time'] );
			$map['off_time'] 	= paramDate( $_POST['off_time']  );
			if ( empty($id) ){
					$this->error('参数有误，未选择对应上架！');
			}
			if(isset($_POST['enable'])) $map['enable'] = 'Y'; else $map['enable'] = 'N';
				
			//参数合法性检查
			$required_field = array(
					//'goods_id'		=> '商品',
					'on_time'	=> '上架日',
					'off_time'	=> '下架日',
					'out_price'=> '买出价格',
					'sort_id'		=> '上架分类'
			);
			foreach ($required_field as $k => $v) {
					if ( empty($map[$k]) ){
							$this->error($v . '不可为空');
					}
			}
			
			/**
			if($map['on_time'] < time()){
					$this->error('上架日期需大于当前日期');
			}
			**/
			
			if($map['off_time'] <= $map['on_time']){
					$this->error('上架日期需大于下架日期');
			}

		  $map['mtime'] 			=	time();
		  //$map['send_number'] 			=	0;
		  //$map['is_delete'] 	= 'N';
		  //$map['insert_admin_id']	=	$this->adminId;
		  $shelfDao = D('GoodsShelf');

		  $id = $shelfDao->where('id='.$id." and is_delete='N' ")->save($map);
		  if(!empty($id)){
					$this->success('修改成功！');
			}else{
					
			}
			$this->error('修改失败！');
	}
	
	
	/**
	 *删除商品下架
	 */
	public function doPutoff() {
		$_POST['ids']	= explode(',', t($_POST['ids']));
		if (empty($_POST['ids'])) {
			echo 0;
			return ;
		}
		$shelfDao = D('GoodsShelf');
		$map['id']	= array('in', $_POST['ids']);
		$data['enable']='N';
		$no = $shelfDao->where($map)->save($data);
		echo $no ? '1' : '0';
	}
	
}